<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row :gutter="12">
        <el-col :span="8">
          <!-- 搜索框 -->
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="clearSearchGoodsInput()"
          >
            <!-- 搜索按钮 -->
            <el-button slot="append" icon="el-icon-search" @click="searchGoodsInfo()"></el-button>
          </el-input>
        </el-col>
        <!-- 添加商品按钮 -->
        <el-col :span="2">
          <el-button type="primary" @click="goAddGoodsPage()">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表区域 -->
      <el-table :data="goodsList" style="width: 100%" border stripe>
        <!-- 索引列 -->
        <el-table-column type="index"></el-table-column>
        <!-- 商品名称 -->
        <el-table-column prop="goods_name" label="商品名称" width="600"></el-table-column>
        <!-- 商品价格 -->
        <el-table-column prop="goods_price" label="商品价格(元)" width="110"></el-table-column>
        <!-- 商品重量 -->
        <el-table-column prop="goods_weight" label="商品重量" width="80"></el-table-column>
        <!-- 创建时间 -->
        <el-table-column label="创建时间" width="160">
          <template slot-scope="scope">{{scope.row.add_time | dataFormat}}</template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 编辑按钮 -->
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <!-- 删除按钮 -->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="delGoods(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="this.queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  // 属性
  data() {
    return {
      // 商品数据列表
      goodsList: [],
      // 商品总数
      total: 0,
      //   查询参数
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
    };
  },
  // 方法
  methods: {
    //   跳转到添加商品页面
    goAddGoodsPage() {
      this.$router.push("/goods/add");
    },
    //   删除商品
    async delGoods(val) {
      // 发起网络请求删除商品信息
      const { data: res } = await this.$http.delete("goods/" + val.goods_id);
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg);
      } else {
        //成功
        this.getGoodsList();
      }
    },
    // 清除搜索商品输入框
    clearSearchGoodsInput() {
      this.queryInfo.query = "";
      this.getGoodsList();
    },
    //   搜索商品信息
    searchGoodsInfo() {
      // 发起网络请求搜索商品
      if (this.queryInfo.query.trim().length === 0) {
        this.queryInfo.query = "";
      } else {
        this.getGoodsList();
      }
    },
    // 获取商品列表
    async getGoodsList() {
      // 发起网络请求
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg);
      } else {
        this.$message.success(res.meta.msg);
        this.goodsList = res.data.goods;
        this.total = res.data.total;
        console.log(res.data);
      }
    },
    // 分页pagesize改变
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getGoodsList();
    },
    // 分页pagenum改变
    handleCurrentChange(newNum) {
      this.queryInfo.pagenum = newNum;
      this.getGoodsList();
    },
  },
  // created
  created() {
    this.getGoodsList();
  },
};
</script>
<style lang="less" scoped>
</style>